package com.muyu.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.utils.ObjUtils;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.product.domain.AsAttributeGroup;
import com.muyu.product.domain.AttributeGroup;
import com.muyu.product.domain.AttributeInfo;
import com.muyu.product.domain.model.AttributeGroupSaveModel;
import com.muyu.product.domain.resp.AttributeGroupPageResp;
import com.muyu.product.mapper.AttributeGroupMapper;
import com.muyu.product.service.AsAttributeGroupService;
import com.muyu.product.service.AttributeGroupService;
import com.muyu.product.service.AttributeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * 属性组Service业务层处理
 *
 * @author DongZeLiang
 * @date 2024-02-27
 */
@Slf4j
@Service
public class AttributeGroupServiceImpl extends ServiceImpl<AttributeGroupMapper, AttributeGroup> implements AttributeGroupService {

    @Autowired
    private AsAttributeGroupService attributeGroupService;

    @Autowired
    private AttributeInfoService attributeInfoService;

    /**
     * 分页查询
     *
     * @param attributeGroupQuery 分组对象
     *
     * @return 返回结果
     */
    @Override
    public TableDataInfo<AttributeGroupPageResp> page (AttributeGroup attributeGroupQuery) {
        List<AttributeGroup> list = this.list(attributeGroupQuery);
        List<AttributeGroupPageResp> pageRespList = list.stream()
                .map(attributeGroup ->
                        AttributeGroupPageResp.groupFunBuild(
                                attributeGroup, groupId -> attributeInfoService.attributeListByGroupId(groupId)
                        )
                )
                .toList();
        return TableDataInfo.<AttributeGroupPageResp>builder()
                .total(new PageInfo(list).getTotal())
                .rows(pageRespList)
                .build();
    }

    /**
     * 查询属性组列表
     *
     * @param attributeGroup 属性组
     *
     * @return 属性组
     */
    @Override
    public List<AttributeGroup> list (AttributeGroup attributeGroup) {
        LambdaQueryWrapper<AttributeGroup> queryWrapper = new LambdaQueryWrapper<>();

        if (ObjUtils.notNull(attributeGroup.getName())) {
            queryWrapper.like(AttributeGroup::getName, attributeGroup.getName());
        }

        if (ObjUtils.notNull(attributeGroup.getStates())) {
            queryWrapper.eq(AttributeGroup::getStates, attributeGroup.getStates());
        }

        return list(queryWrapper);
    }

    /**
     * 保存
     *
     * @param attributeGroupSaveModel 属性组保存模型
     *
     * @return 是否成功
     */
    @Override
    @Transactional
    public Boolean save (AttributeGroupSaveModel attributeGroupSaveModel) {
        AttributeGroup attributeGroup = attributeGroupSaveModel.buildAttributeGroup();
        boolean save = this.save(attributeGroup);
        // 处理属性ID
        Long attributeGroupId = attributeGroup.getId();
        List<Long> attributeIdList = attributeGroupSaveModel.getAttributeIdList();

        attributeGroupService.saveBatch(
                attributeIdList.stream()
                        .map(attributeId -> AsAttributeGroup.buildGroup(attributeGroupId, attributeId))
                        .toList()
        );
        return save;
    }
}
